$(function() {

    'use strict';

    var remote = {

        _doAjax: function(data, success) {

            return $.ajax({
                url      : 'http://www.huihaicenter.com/api/uc_yd/api.php',
                data     : data,
                dataType : 'jsonp',

                success: function(resp) {

                    if (resp.status) {
                        success && success(resp);
                    } else {
                        alert(resp.msg);
                    }
                },

                error: function() {
                    alert('Network error, please try again.');
                }
            });
        },

        _fakeAjax: function(success, data) {

            return setTimeout(function() {
                success(data);
            }, 300);
        },

        inc: function(id, success) {
            // return this._fakeAjax(success);
            return this._doAjax({
                action : 'inc',
                id     : id
            }, success);
        },

        doVote: function(id, success) {
            // console.log(id)
            // return this._fakeAjax(success);
            return this.inc(id, success);
        }
    };

    // 51-61  是人物的投票信息

    // 70    是总pv
    // 71-80 是渠道pv

    var channelIds = {
        'Splash'            : 71,
        'TopBannerU3'       : 72,
        'Push'              : 73,
        'SearchHotwordU3'   : 74,
        'Menu'              : 75,
        'SpeedDialU3'       : 76,
        'SpeedDialMini'     : 77,
        'TopBannerMini'     : 78,
        'SearchHotwordMini' : 79
    };

// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=Splash
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=TopBannerU3
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=Push
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=SearchHotwordU3
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=Menu
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=SpeedDialU3
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=SpeedDialMini
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=TopBannerMini
// http://www.huihaicenter.com/www/projects/uc/bigg2/?channel=SearchHotwordMini

    // 总pv和渠道pv统计
    var stat = {

        incChannel: function(name) {

            var cId = channelIds[name];

            if (cId) {
                remote.inc(cId);
                _czc.push(﻿['_trackEvent', 'channel', name]);
            }

            return this;
        },

        incPv: function() {
            remote.inc(70);
            _czc.push(﻿['_trackEvent', 'channel', 'total']);
            return this;
        }
    };

    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'),
            r = window.location.search.substr(1).match(reg);
        return (r != null) ? unescape(r[2]) : null;
    }

    stat.incPv();
    stat.incChannel(getQueryString('channel'));

    $('.download-window').click(function() {
        window.open('market://details?id=com.uc.iflow&referrer=utm_source%3D' + getQueryString('channel'));
        return false;
    });

// if (false) {
    if (!/uc/i.test(navigator.userAgent)) {

        $('.download-cover').show().click(function() {
            $('.download-pop').show();
            return false;
        });

        $('.download-shadow').on('touchstart', function() {
            $('.download-pop').hide();
            return false;
        });
    }

    var page = {

        _switchTo: function(name) {
            $('.page').hide();
            $('.page-' + name).show();
            $('body, html').scrollTop(0);
            return this;
        },

        _setBtnVoted: function(id) {
            $('.vote-' + id).addClass('disabled').html('Voted');
            return this;
        },

        _bindEvents: function() {

            var that = this;

            $('.back-btn').click(function() {
                that._switchTo('home');
                return false;
            });

            $('.tc-btn').click(function() {
                that._switchTo('rule');
                return false;
            });

            function recordVote(id) {

                for (var i = 0; i < 3; ++i) {

                    if (!localStorage['vote' + i]) {

                        localStorage['vote' + i] = id;

                        if (i >= 2) {
                            localStorage.voted = true;
                        }

                        break;
                    }
                }
            }

            $('.page-home .vote-btn').click(function() {

                var el = $(this),
                    id = el.data('id');

                if (!el.hasClass('disabled')) {

                    remote.doVote(id, function() {

                        that._setBtnVoted(id);
                        recordVote(id);

                        if (localStorage.voted) {
                            $('.vote-btn').addClass('disabled');
                        }
                    });

                    _czc.push(﻿['_trackEvent', 'vote', id]);
                }

                return false;
            });

            return this;
        },

        init: function() {

            for (var i = 0, id; i < 3; ++i) {

                id = localStorage['vote' + i];

                if (id) {
                    this._setBtnVoted(id);
                }
            }

            if (localStorage.voted) {
                $('.vote-btn').addClass('disabled');
            }

            return this._bindEvents()._switchTo('home');
        }
    };

    page.init();

});
